package com.qf.controller;

import com.qf.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

/**
 * 搜索 微服务
 * @author lixu
 */
@RestController
@RequestMapping("/search")
public class SearchController {

    @Value("${server.port}")
    private String serverPort;


    /**
     * 模拟根据id查询
     * @param id
     */
    @GetMapping("/{id}")
    public Customer findById(@PathVariable Integer id) throws Exception {
        //int i = 1/0;
        System.out.println(Thread.currentThread().getName());
        //年龄使用随机数, 这样可以看到缓存是否生效
        return new Customer(id,"张三", (int) (Math.random() * 100000));
    }



    /**
     * 测试
     */
    @GetMapping("/find")
    public String find(){

/*        if(serverPort.equalsIgnoreCase("9002")){
            //9002  响应时间少
        }else{

            //9012响应时间长
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }*/

        return "我是搜索微服务:" + serverPort;
    }

    /**
     * 测试
     *  http://localhost:9001/search/findByName?name=?&age=?&flag=?
        //@RequestParam :  String name = request.getParamter(name)
        //@RequestParam :  String age = request.getParamter(age)  Integer age = Integer.parseInt(age)

     required = false : 不必须
     */
    @GetMapping("/findByName")
    public String findByName(@RequestParam(required = false) String name,
                             @RequestParam(required = false) Integer age,
                             @RequestParam(required = false)  Boolean flag){

        return "@RequestParam我是搜索微服务:" + serverPort + ":" + name + ":" + age + ":" + flag;
    }
    /**
     * 测试
     *  rest风格  restFul风格
     * http://localhost:9001/search/findByName/name/age/flag
     */
    @GetMapping("/findByPath/{name}/{age}/{flag}")
    public String findByPath(
            @PathVariable(value = "name") String name,
            @PathVariable(value = "age") Integer age,
            @PathVariable(value = "flag") Boolean flag
                             ){

        return "@PathVariable我是搜索微服务:" + serverPort + ":" + name + ":" + age + ":" + flag;
    }

    /**
     *  接收JSON串
     */
    @PostMapping("/findRequestBody")
    public Customer findRequestBody(@RequestBody Customer customer){
        customer.setId(888888);

        return customer;
    }

}
